{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "59428e05",
   "metadata": {},
   "source": [
    "# Cloudflare Workers AI\n",
    "\n",
    ">[Cloudflare, Inc. (Wikipedia)](https://en.wikipedia.org/wiki/Cloudflare) is an American company that provides content delivery network services, cloud cybersecurity, DDoS mitigation, and ICANN-accredited domain registration services.\n",
    "\n",
    ">[Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/) allows you to run machine learning models, on the `Cloudflare` network, from your code via REST API.\n",
    "\n",
    ">[Cloudflare AI document](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) listed all text embeddings models available.\n",
    "\n",
    "## Setting up\n",
    "\n",
    "Both Cloudflare account ID and API token are required. Find how to obtain them from [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f60023b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "\n",
    "my_account_id = getpass.getpass(\"Enter your Cloudflare account ID:\\n\\n\")\n",
    "my_api_token = getpass.getpass(\"Enter your Cloudflare API token:\\n\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c94b531-f5a6-4eea-9f08-a85e9d3bff28",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92c5b61e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.embeddings.cloudflare_workersai import (\n",
    "    CloudflareWorkersAIEmbeddings,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "062547b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embeddings = CloudflareWorkersAIEmbeddings(\n",
    "    account_id=my_account_id,\n",
    "    api_token=my_api_token,\n",
    "    model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
    ")\n",
    "# single string embeddings\n",
    "query_result = embeddings.embed_query(\"test\")\n",
    "len(query_result), query_result[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1dcc4bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 384)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# string embeddings in batches\n",
    "batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
    "len(batch_query_result), len(batch_query_result[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52de8b88",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "7377c2ccc78bc62c2683122d48c8cd1fb85a53850a1b1fc29736ed39852c9885"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
